package com.cow.net.tcp;

import com.csgyy.Constraint;
import com.csgyy.tcp.domain.DeviceValue;
import com.csgyy.tcp.domain.TypeData;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

import java.util.Date;

@Sharable
public class HeartBeatClientHandler extends ChannelInboundHandlerAdapter {

    
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("激活时间是："+new Date());
        System.out.println("HeartBeatClientHandler channelActive");
        ctx.fireChannelActive();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("停止时间是："+new Date());
        System.out.println("HeartBeatClientHandler channelInactive");
    }


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        DeviceValue.Request message = (DeviceValue.Request) msg;

        System.out.println(message);

        DeviceValue.Request.Builder deviceValue  =DeviceValue.Request.newBuilder();
       if(message.getPackageType() != TypeData.PING ||(message.getDataType() == TypeData.DATA && message.getPackageType() == TypeData.REPLY)) {
           deviceValue.setPackageType(TypeData.PING); //数据数据类型
           deviceValue.setDataType(TypeData.DATAFDJ); //发动机类型
           deviceValue.setContext("ok");
           deviceValue.setCompanyId(Constraint.companyId);
           ctx.write(deviceValue);
           ctx.flush();
       }






        ReferenceCountUtil.release(msg);
    }
}
